package com.service.school.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TClass;
import com.model.TSpecial;
import com.model.TStudent;
import com.model.TTeacher;
import com.pageModel.ClassInfo;
import com.pageModel.DataGrid;
import com.pageModel.Special;
import com.service.school.SpecialServiceI;

@Service(value = "specialService")
public class SpecialServiceImpl implements SpecialServiceI {

	private BaseDaoI<TSpecial> specialDao;

	public BaseDaoI<TSpecial> getSpecialDao() {
		return specialDao;
	}
	@Autowired
	public void setSpecialDao(BaseDaoI<TSpecial> specialDao) {
		this.specialDao = specialDao;
	}
	private BaseDaoI<TTeacher> teacherDao;

	public BaseDaoI<TTeacher> getTeacherDao() {
		return teacherDao;
	}

	@Autowired
	public void setTeacherDao(BaseDaoI<TTeacher> teacherDao) {
		this.teacherDao = teacherDao;
	}
	private BaseDaoI<TStudent> studentDao;

	public BaseDaoI<TStudent> getStudentDao() {
		return studentDao;
	}

	@Autowired
	public void setStudentDao(BaseDaoI<TStudent> studentDao) {
		this.studentDao = studentDao;
	}
	
	@Override
	public DataGrid datagrid(Special special) {
		DataGrid dg=new DataGrid();
		String hql="from TSpecial t ";
		if(special.getSchoolmateName()!=null&&!special.getSchoolmateName().trim().equals("")){
			hql+="where t.schoolmateName like '%"+special.getSchoolmateName().trim()+"%'";
		}
		String totalHql = "select count(*) " + hql;
		List<TSpecial> l=specialDao.find(hql,special.getPage(),special.getRows());
		List<Special> nl=new ArrayList<Special>();
		if(l!=null&&l.size()>0){
			for(TSpecial t:l){
				Special u=new Special();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(specialDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}
	@Override
	public void edit_TG(Special special) {
		TSpecial t=specialDao.get(TSpecial.class, special.getSchoolmateId());
		t.setIsSpecial("启用");
		specialDao.update(t);
		
	}
	@Override
	public void edit_BTG(Special special) {
		String hql="delete TSpecial t where t.schoolmateId in('"+special.getSchoolmateId()+"')";
		specialDao.executeHql(hql);
		if(special.getSchoolmateType().trim().equals("学生")){
			studentDao.executeHql("update TStudent t set t.isSpecial ='否' where t.stuId='"+special.getSchoolmateId()+"'");
		}
		if(special.getSchoolmateType().trim().equals("教师")){
			teacherDao.executeHql("update TTeacher t set t.isSpecial ='否' where t.teacherId='"+special.getSchoolmateId()+"'");
		}
	}
	@Override
	public void add(Special special) {
		TSpecial t=new TSpecial();
		t.setId(UUID.randomUUID().toString());
		t.setSchoolmateId(special.getSchoolmateId());
		t.setSchoolmateName(special.getSchoolmateName());
		t.setSchoolmateSex(special.getSchoolmateSex());
		t.setSchoolmateCollege(special.getSchoolmateCollege());
		t.setSchoolmateBorn(special.getSchoolmateBorn());
		t.setSchoolmateType(special.getSchoolmateType());
		t.setIsSpecial("启用");
		specialDao.save(t);
		if(special.getSchoolmateType().trim().equals("学生")){
			studentDao.executeHql("update TStudent t set t.isSpecial ='是' where t.stuId='"+special.getSchoolmateId()+"'");
		}
		if(special.getSchoolmateType().trim().equals("教师")){
			teacherDao.executeHql("update TTeacher t set t.isSpecial ='是' where t.teacherId='"+special.getSchoolmateId()+"'");
		}
	}
	@Override
	public List<TSpecial> getSpecialCmb(Special special) {
		String hql="from TSpecial t ";
		List<TSpecial> l = specialDao.find(hql);
		return l;
	}
	
}
